﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using GONES.Tools.Common;
using Newtonsoft.Json;

namespace GONES.BLL
{
    public class S_DbBackUpManager : IS_DbBackUpManager
    {
        public IS_DbBackUpDao S_DbBackUpDao;
        protected static readonly ILog Log =
             LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public IS_LogDao S_LogDao;
        private readonly IUserInfo _userInfo;
        public S_LogManager S_LogManager;

        public S_DbBackUpManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }

        public string ErrMsg { get; set; }

        public S_DbBackUp Get(string id)
        {
            return S_DbBackUpDao.Get(id);
        }

        public long dbBackDataBase(string sql)
        {
            return S_DbBackUpDao.ExecHqlResultLong(sql);
        }

        public void Add(S_DbBackUp dbback)
        {
            if (IsError(dbback))
                return;
            dbback.CreateTime = System.DateTime.Now;
            S_DbBackUpDao.Save(dbback);
            SaveLog("数据库备份管理", "数据库备份", "系统管理", dbback);
   
        }

        public void Edit(S_DbBackUp dbback)
        {
            if (IsError(dbback))
               return ;
            dbback.ModifyTime = System.DateTime.Now;
            S_DbBackUpDao.Update(dbback);
            SaveLog("数据库备份管理", "数据库备份", "系统管理", dbback);
        }

        public void Delete(string id)
        {
            S_DbBackUp dbback = S_DbBackUpDao.Get(id);
            S_DbBackUpDao.Delete(dbback);
            SaveLog("数据库备份管理", "数据库备份删除", "系统管理", dbback);
        }

     

        public IList<S_DbBackUp> List(IList<DataFilter> filters, int pageindex, int pagesize, ref long recordcount)
        {
            string strFilter = DataOperate.GetHqlstrByExtFilter(filters, "e");

            recordcount = S_DbBackUpDao.ExecHqlResultLong("SELECT COUNT(*) FROM S_DbBackUp e where 1=1 " +
                                               (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter));

            return S_DbBackUpDao.GetHqlList("SELECT e FROM S_DbBackUp e  where 1=1 " +
                                       (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter) +
                                       " ORDER BY e.ID DESC", (pageindex - 1) * pagesize,
                                       pagesize);
        }

        private void SaveLog(string module, string operate, string logtype, S_DbBackUp dbback)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:备份文件名是{1}，备份时间是{2}", operate, dbback.FileName, dbback.BackUpDate);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(dbback)));
        }

        protected bool IsError(S_DbBackUp dbback)
        {
            if (string.IsNullOrEmpty(dbback.FileName))
            {
                ErrMsg = "文件名不能为空！";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }
        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（系统管理、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
